/** @file    MediaList.cpp
 *  @author  Alessandro Polo
 *  @version $Id: MediaList.cpp 2829 2010-08-02 20:45:47Z alex $
 *  @brief
 * File containing methods for the wosh::entertainment::MediaList class.
 * The header for this class can be found in MediaList.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include <framework/entertainment/MediaList.h>
 #include <core/ObjectAllocator.h>
 #include <core/List.h>
 #include <core/Table.h>
 #include <core/Functional.h>
 #include <core/DataModels.h>


using namespace std;
namespace wosh {
 namespace entertainment {

 WOSH_REGISTER(wosh::entertainment::MediaList, "wosh::Object", 1.0, _static_MediaList )

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS

MediaList::MediaList()
	: Object() {
	clear();
 }
MediaList::MediaList( const MediaList& m )
	: Object(m) {
	*this = m;
 }

//////////////////////////////////////////////////////////////////////////////////////////////// CONSTRUCTORS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void MediaList::clear() {
	this->Properties.clear();

	setCurrentIndex(-1);
	setCurrentPosition(-1);

	this->tracks.clear();
 }

WRESULT MediaList::remove( int index ) {
	MutexLockerWrite mL(this->tracks.getMutex());
	if ( index < 0 ) return WRET_ERR_PARAM;
	if ( index >= (int)this->tracks.size() ) return WRET_ERR_PARAM;

	tMediaTrackList::Iterator it = this->tracks.find(index);
	if ( it == this->tracks.end() )
		return WRET_ERR_PARAM;
	this->tracks.getVector().erase(it);
	if ( index < this->currentIndex )
		setCurrentIndex(this->currentIndex-1);

	return WRET_OK;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool MediaList::hasNext() const {
	if ( this->tracks.count() == 0 )
		return false;
	if ( this->currentIndex >= (int)this->tracks.count() )
		return false;
	return true;
 }

MediaTrack* MediaList::getCurrentTrack() {
	return this->tracks.itemAt(this->currentIndex);
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void MediaList::setCurrentIndex( int value ) {
	this->currentIndex = value;
	Properties.set(_MediaList_KEY_CurrentIndex, value );
 }
void MediaList::setCurrentPosition( long value ) {
	this->currentPosition = value;
	Properties.set(_MediaList_KEY_CurrentPosition, value );
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void MediaList::setCurrentTrack( const ResourcePath& fullpath ) {
	this->tracks.transactionBeginWrite();
	this->currentIndex = 0;
	tMediaTrackList::Iterator it;
	const tMediaTrackList::ConstIterator it_end = this->tracks.end();
	for ( it=this->tracks.begin(); it!=it_end; ++it, ++this->currentIndex ) {
		if ( *it == NULL ) continue;
		if ( (*it)->getTrack() == fullpath )
			break;
	 }
	this->tracks.transactionEnd();
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

MediaList& MediaList::operator=(const MediaList& m) {
	if (this == &m) return *this; // same object?
	(Object&)*this = (Object&)m;

	this->tracks.transactionBeginWrite();
	this->tracks.free();
	m.tracks.transactionBeginRead();
	tMediaTrackList::ConstIterator it;
	const tMediaTrackList::ConstIterator it_end = m.tracks.end();
	for ( it=m.tracks.begin(); it!=it_end; ++it ) {
		if ( *it == NULL ) continue;
		this->tracks.push_back( (*it)->clone() );
	 }
	m.tracks.transactionEnd();
	this->tracks.transactionEnd();

	this->currentIndex = m.currentIndex;
	this->currentPosition = m.currentPosition;
	this->Properties = m.Properties;
	return *this;
 }

bool MediaList::operator==(const MediaList &other) const {
	if ( this->tracks.count() != other.tracks.count() )
		return false;
	return false;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool MediaList::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	tMediaTrackList::ConstIterator it;
	const tMediaTrackList::ConstIterator it_end = this->tracks.end();
	for ( it=this->tracks.begin(); it!=it_end; ++it ) {
		if ( *it == NULL ) continue;
		ret = (*it)->updateHash(hash_context) & ret;
	 }
	ret = hash_context->update( this->currentIndex ) & ret;
	ret = hash_context->update( this->currentPosition ) & ret;
	ret = this->Properties.updateHash(hash_context) & ret;
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DataFieldCollector<MediaList>* MediaList::getDataFields() {
	DataFieldCollector<MediaList>* fields = new DataFieldCollector<MediaList>();
	fields->add<const std::string&, Variant>(&MediaList::getName, "Name", 0);
	fields->add<int, Variant>(&MediaList::getCurrentIndex, "CurrentIndex", 1);
	fields->add<long, Variant>(&MediaList::getCurrentPosition, "CurrentPosition", 2);
	fields->add<int, Variant>(&MediaList::size, "Count", 3);
	fields->add<const URI&,URI>(&MediaList::getURI, "URI", 4);
	fields->add<const std::string&,Variant>(&MediaList::getEntityID, "EntityID", 5, false);
	fields->add<const Permission&,Permission>(&MediaList::getPermission, "Permission", 6, false);
	fields->add<const char*,Variant>(&MediaList::getClassName, "Type", 7);
	return fields;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

List* MediaList::toList() const {
	this->tracks.transactionBeginRead();
	List* objs = new List( this->tracks.size() );
	tMediaTrackList::ConstIterator it;
	const tMediaTrackList::ConstIterator it_end = this->tracks.end();
	for ( it=this->tracks.begin(); it!=it_end; ++it ) {
		if ( *it == NULL ) continue;
		objs->push_back<Variant>( (*it)->getTrack().getPathFull() );
	 }
	this->tracks.transactionEnd();
	return objs;
 }

Table* MediaList::toTable() const {
	this->tracks.transactionBeginRead();
	Table* tbl = new Table( this->tracks.size(), 3, true );
	tbl->setTableName("PlayList");
	tbl->setHeaderHorizontal("Index", 0);
	tbl->setHeaderHorizontal("URL", 1);
	tbl->setHeaderHorizontal("Info", 2);
	unsigned int iRow = 0;
	tMediaTrackList::ConstIterator it;
	const tMediaTrackList::ConstIterator it_end = this->tracks.end();
	for ( it=this->tracks.begin(); it!=it_end; ++it, ++iRow ) {
		if ( *it == NULL ) continue;
		//alf bugfix 588218 begin
		//tbl->set<Variant>( (int)(iRow-1), iRow, 0);
		tbl->set<Variant>( (int)iRow, iRow, 0);
		//alf bugfix 588218 end
		tbl->set<Variant>( (*it)->getTrack().getPath(), iRow, 1);
//--
	 }
	this->tracks.transactionEnd();
	return tbl;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace entertainment
}; // namespace wosh
